package algotithm.leetcode.dfs.test690;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

class Employee {
    int id;
    int importance;
    List<Integer> subordinates;
}
/**
 * @author 25068
 * @Date 2021/5/1 8:44
 * @Title 690. 员工的重要性
 */
public class Solution{

    public static void main(String[] args) {
        List<Employee> employees = new ArrayList<>();
        Employee e1 = new Employee();
        e1.id = 1;
        e1.importance = 5;
        e1.subordinates = new ArrayList<>();
        e1.subordinates.add(2);
        e1.subordinates.add(3);
        Employee e2 = new Employee();
        e2.id = 2;
        e2.importance = 3;
        e2.subordinates = new ArrayList<>();
        Employee e3 = new Employee();
        e3.subordinates = new ArrayList<>();
        e3.id = 3;
        e3.importance = 3;
        employees.add(e1);
        employees.add(e2);
        employees.add(e3);

        Solution solution = new Solution();
        int id = 1;
        int ans = solution.getImportance(employees,id);
        System.out.println(ans);
    }

    private HashMap<Integer,Employee> map;
    private int getImportance(List<Employee> employees, int id) {
        map = new HashMap<>(1024);
        for (Employee e : employees) {
            map.put(e.id,e);
        }
        return dfs(id);
    }

    public int dfs(int id) {
        Employee e = map.get(id);
        int ans = e.importance;
        for (int t : e.subordinates) {
            ans += dfs(t);
        }
        return ans;
    }

}
